package org.ccay.core.log;

import java.text.MessageFormat;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * ILogger接口的Log4j实现，委托Logger进行实际的日志记录
 */
public class Log4jLogger implements ILogger {
	
	/** log4j2的日志实例类 */
	private Logger logger;
	
	/** Fully qualified name **/
	private static final String FQCN = Log4jLogger.class.getName();

	/** The factory method of Log4jLogger **/
	public static ILogger getLog(String name) {
		return new Log4jLogger(Logger.getLogger(name));
	}

	/**
	 * @param name
	 */
	public Log4jLogger(Logger logger) {
		this.logger = logger;
	}

	public void debug(String message) {
		logger.log(FQCN, Level.DEBUG, message, null);
	}

	public void debug(String message, Object... params) {
		if (logger.isEnabledFor(Level.DEBUG)){
			debug(MessageFormat.format(message, params));
		}
	}

	public void debug(Throwable t) {
		debug(null, t);
	}

	public void debug(String message, Throwable t) {
		logger.log(FQCN, Level.DEBUG, message, t);
	}

	public void debug(String message, Object[] params, Throwable t) {
		if (logger.isEnabledFor(Level.DEBUG)){
			debug(MessageFormat.format(message, params), t);
		}
	}

	public void info(String message) {
		logger.log(FQCN, Level.INFO, message, null);
	}

	public void info(String message, Object... params) {
		if (logger.isEnabledFor(Level.INFO)){
			info(MessageFormat.format(message, params));
		}
	}

	public void info(Throwable t) {
		info(null, t);
	}

	public void info(String message, Throwable t) {
		logger.log(FQCN, Level.INFO, message, t);
	}

	public void info(String message, Object[] params, Throwable t) {
		if (logger.isEnabledFor(Level.INFO)){
			info(MessageFormat.format(message, params), t);
		}
	}

	public void warn(String message) {
		logger.log(FQCN, Level.WARN, message, null);
	}

	public void warn(String message, Object... params) {
		if (logger.isEnabledFor(Level.WARN)){
			warn(MessageFormat.format(message, params));
		}
	}

	public void warn(Throwable t) {
		warn(null, t);
	}

	public void warn(String message, Throwable t) {
		logger.log(FQCN, Level.WARN, message, t);
	}

	public void warn(String message, Object[] params, Throwable t) {
		if (logger.isEnabledFor(Level.WARN)){
			warn(MessageFormat.format(message, params), t);
		}
	}

	public void error(String message) {
		logger.log(FQCN, Level.ERROR, message, null);
	}

	public void error(String message, Object... params) {
		if (logger.isEnabledFor(Level.ERROR)){
			error(MessageFormat.format(message, params));
		}
	}

	public void error(Throwable t) {
		error(null, t);
	}

	public void error(String message, Throwable t) {
		logger.log(FQCN, Level.ERROR, message, t);
	}

	public void error(String message, Object[] params, Throwable t) {
		if (logger.isEnabledFor(Level.ERROR)){
			error(MessageFormat.format(message, params), t);
		}
	}

	public void fatal(String message) {
		logger.log(FQCN, Level.FATAL, message, null);
	}

	public void fatal(String message, Object... params) {
		if (logger.isEnabledFor(Level.FATAL)){
			fatal(MessageFormat.format(message, params));
		}
	}

	public void fatal(Throwable t) {
		fatal(null, t);
	}

	public void fatal(String message, Throwable t) {
		logger.log(FQCN, Level.FATAL, message, t);
	}

	public void fatal(String message, Object[] params, Throwable t) {
		if (logger.isEnabledFor(Level.FATAL)){
			fatal(MessageFormat.format(message, params), t);
		}
	}

	public void trace(String message) {
		logger.log(FQCN, Level.TRACE, message,null);
	}


	public void trace(String message, Object... params) {
		if (logger.isEnabledFor(Level.TRACE)){
			trace(MessageFormat.format(message, params));
		}
	}

	public boolean isDebugEnabled() {
		return logger.isDebugEnabled();
	}

}
